package com.blueocean_health.care.management.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.domain.base.Hospital;
import com.blueocean_health.care.management.domain.base.Team;
import com.blueocean_health.care.management.domain.dto.HospitalDto;
import com.blueocean_health.care.management.domain.dto.PullDownDto;
import com.blueocean_health.care.management.exception.OrderException;
import com.blueocean_health.care.management.mapper.HospitalMapper;
import com.blueocean_health.care.management.mapper.TeamMapper;
import com.blueocean_health.care.management.service.HospitalService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 医院模块 业务实现类 2017年8月22日17:06:43
 * 
 * @author wuyanli
 *
 */
@Service
public class HospitalServiceImpl implements HospitalService {

	@Autowired
	private HospitalMapper hospitalDao;
	@Autowired
	private TeamMapper teamMapper;

	// 用户端静态图片地址
	@Value("${service.address.hospitalpic}")
	private String serviceAddress;

	/**
	 * 获取所有医院信息
	 */
	@Override
	public List<PullDownDto> getAllHospital() {
		// 医院下拉列表
		List<PullDownDto> allHospital = hospitalDao.getAllHospital();
		return allHospital;
	}

	/**
	 * 获取所有医院信息
	 * 
	 * @throws OrderException
	 */
	@SuppressWarnings("unlikely-arg-type")
	@Override
	public List<PullDownDto> getNewAllHospital(String teamId) throws OrderException {
		// 1.获取当前组织的所属一级组织ID
		Team team = teamMapper.selectByPrimaryKey(Integer.parseInt(teamId));
		if (StringUtils.isEmpty(team)) {
			throw new OrderException("当前项目不存在");
		}

		// 2.根据所属一级组织ID,查询所有的组织
		List<PullDownDto> allHospital = teamMapper.getAllProjectByFirstOrg(team.getFirstOrg());
		if (StringUtils.isEmpty(hospitalDao)) {
			return allHospital;
		}

		// 3.过滤当前组织
		allHospital = allHospital.stream().filter(hos -> {
			if (hos.getValue().equals(String.valueOf(team.getTeamId()))) {
				return false;
			}
			return true;
		}).collect(Collectors.toList());

		return allHospital;
	}

	@Override
	public Hospital getHospitalInfo(String hospitalId) {
		Hospital hospitalInfo = hospitalDao.getHospitalInfo(hospitalId);
		return hospitalInfo;
	}

	@Override
	public PageInfo<HospitalDto> getHospitalList(Map<String, String> map) {
		int pageSize = Integer.parseInt(map.get("pageSize"));
		int page = Integer.parseInt(map.get("page"));
		PageHelper.startPage(page, pageSize);
		List<HospitalDto> hospitalList = hospitalDao.getHospitalList(map);
		for (HospitalDto hospitalDto : hospitalList) {
			// 拼接静态资源路径
			hospitalDto.setHospitalPic(serviceAddress + hospitalDto.getHospitalPic());
		}
		PageInfo<HospitalDto> pageInfo = new PageInfo<HospitalDto>(hospitalList);
		return pageInfo;
	}

	/**
	 * 根据医院id获取医院电话
	 */
	@Override
	public String getHospitalPhone(String hospitalId) {
		Hospital hospitalInfo = hospitalDao.getHospitalInfo(hospitalId);
		return hospitalInfo.getHospitalPhone();
	}
}
